//
//  WBHomeViewController.swift
//  CZweibo
//
//  Created by jyh on 2017/12/9.
//  Copyright © 2017年 jyh. All rights reserved.
//

import UIKit

// cell ID
fileprivate let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }
    
    /// 加载数据
    override func loadData() {

        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            
            self.refreshControl?.endRefreshing()
            
            // 恢复上拉刷新标记
            self.isPullup = false
            
            if shouldRefresh {
                
                self.tableView?.reloadData()
            }
        }
        
    }

    @objc fileprivate func showFriends() {
        nsLog("好友")
        
        let vc = WBCSViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
}

// MARK: - tableView数据源
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        

        cell.viewModel = listViewModel.statusList[indexPath.row]
        
        
        return cell
    }
}

// MARK: - 设置UI
extension WBHomeViewController {
    
    override func setupTableView() {
        super.setupTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action:  #selector(showFriends), isBack: false)
        
        // 注册cell
        
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: cellId)
        
        // 设置行高
        tableView?.rowHeight = UITableViewAutomaticDimension
        // 预估行高
        tableView?.estimatedRowHeight = 300
        // 取消分割线
        tableView?.separatorStyle = .none
        
        setupNavTitle()
    }
    
    private func setupNavTitle() {
        
        let title = WBNetWorkManager.shared.userAccount.screen_name
        
        let button = WBTitleButton(title: title)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc func clickTitleButton(btn: UIButton) {
        
        btn.isSelected = !btn.isSelected
    }
    
}
